import { ACCESS_TOKEN, USER_ID } from "@/store/mutation-types";
import storage from "@/utils/storage";
import * as LoginApi from "@/api/login";
import { getUserInfo } from "@/api/user";
import { navTo } from "@/core/app";

// 登陆成功后执行
const loginSuccess = (commit, data) => {
  // 过期时间30天
  const expiryTime = 30 * 86400;
  // 保存tokne和userId到缓存
  storage.set(USER_ID, data.result.userId, expiryTime);
  storage.set(ACCESS_TOKEN, data.data, expiryTime);
  // 记录到store全局变量
  commit("SET_TOKEN", data.data);
  commit("SET_USER_ID", data.result.userId);
  commit("SET_USER_INFO", data.result);
  navTo("/pages/index/index");
};

const user = {
  state: {
    // 用户认证token
    token: "",
    // 用户ID
    userId: null,
    userInfo: uni.getStorageSync("userInfo") || {},
  },

  mutations: {
    SET_TOKEN: (state, value) => {
      state.token = value;
    },
    SET_USER_ID: (state, value) => {
      state.userId = value;
    },
    SET_USER_INFO: (state, value) => {
      state.userInfo = value;
      console.log("value :>> ", value);
      uni.setStorageSync("userInfo", value);
    },
  },

  actions: {
    // 用户登录(普通登录: 输入手机号和验证码)
    Login({ commit }, data) {
      return new Promise((resolve, reject) => {
        LoginApi.login(data)
          .then((response) => {
            if (response.status == 0) {
              const result = {
                token: response.data,
                userId: response.result.userId,
                userInfo: response.result,
              };
              loginSuccess(commit, result);
              resolve(response);
            } else {
              reject(response);
            }
          })
          .catch(reject);
      });
    },

    // 微信小程序一键授权登录(获取用户基本信息)
    LoginMpWx({ commit }, data) {
      return new Promise((resolve, reject) => {
        LoginApi.loginMpWx({ form: data }, { isPrompt: false })
          .then((response) => {
            if (response.status == 0) {
              loginSuccess(commit, response);
              resolve(response);
            } else {
              reject(response);
            }
          })
          .catch(reject);
      });
    },

    // 微信小程序一键授权登录(授权手机号)
    LoginMpWxMobile({ commit }, data) {
      return new Promise((resolve, reject) => {
        LoginApi.loginMpWxMobile(data)
          .then((response) => {
            if (response.status == 0) {
              loginSuccess(commit, response);
              resolve(response);
            } else {
              reject(response);
            }
          })
          .catch(reject);
      });
    },

    // 退出登录
    Logout({ commit }, data) {
      const store = this;
      return new Promise((resolve, reject) => {
        if (store.getters.userId > 0) {
          // 删除缓存中的tokne和userId
          storage.remove(USER_ID);
          storage.remove(ACCESS_TOKEN);
          // 记录到store全局变量
          commit("SET_TOKEN", "");
          commit("SET_USER_ID", null);
          resolve();
        }
      });
    },
  },
};

export default user;
